<?php

namespace App\Listeners;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SqlListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  QueryExecuted $event
     * @return void
     */
    public function handle(QueryExecuted $event)
    {
        //在开发环境记录sql查询日志
        if (config('app.env') !== 'production') {
            $sql = str_replace("?", "'%s'", $event->sql);

            $log = vsprintf($sql, $event->bindings);

            $log = '[' . date('Y-m-d H:i:s') . '] ' . $log . "\r\n";
            $filepath = storage_path('logs\sql.log');
            file_put_contents($filepath, $log, FILE_APPEND);
        }
    }
}
